package com.xuelongjiang.mqdemo.mq;

import com.alibaba.fastjson.JSON;
import com.xuelongjiang.domain.Orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xuelongjiang
 */
@Component
public class HelloSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitClient rabbitClient;

    @Value("${mq.queues.order.name}")
    private String orderQueue;


    @Value("${mq.topic.order.name}")
    private String orderTopic;

    @Value("${mq.queues.order.key.name}")
    private String bindKey;

    private  static final Logger LOGGER = LoggerFactory.getLogger(HelloSender.class);

    public void send(String message){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");


        String context =  message +"--"+ "hello" +"--"+ format.format(new Date());
        LOGGER.info("sender:{}",context);
        //this.amqpTemplate.convertAndSend("hello", context);

        //this.rabbitTemplate.convertAndSend("hello", context);

        rabbitClient.sendMesgToQuence("hello", context);
    }


    public void sendOrders(Orders orders){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");


        String context = JSON.toJSONString(orders);
        LOGGER.info("sender orders:{}",context);

        //this.rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback());

        //this.rabbitTemplate.convertAndSend(orderTopic, bindKey, context);
        rabbitClient.sendToTopic(orderTopic, bindKey, context);

    }


}
